const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()

exports.main = async (event, context) => {
  console.log('getActivityDetail 云函数被调用，参数：', event)
  
  try {
    const { activityId } = event
    
    if (!activityId) {
      return {
        success: false,
        message: '活动ID不能为空'
      }
    }
    
    const activitiesCollection = db.collection('activities')
    
    // 根据活动ID查询详情
    const result = await activitiesCollection.doc(activityId).get()
    
    if (!result.data) {
      return {
        success: false,
        message: '活动不存在'
      }
    }
    
    console.log(`获取活动详情成功，活动ID: ${activityId}`)
    
    // 确保返回地理位置信息
    const activityData = result.data
    const responseData = {
      ...activityData,
      // 确保包含地理位置字段
      latitude: activityData.latitude || null,
      longitude: activityData.longitude || null,
      address: activityData.address || activityData.location || '',
      location: activityData.location || ''
    }
    
    return {
      success: true,
      data: responseData,
      message: '活动详情获取成功'
    }
    
  } catch (error) {
    console.error('获取活动详情失败:', error)
    return {
      success: false,
      error: error.message,
      message: '获取活动详情失败'
    }
  }
}